const mysql = require('../db/index')
//导入bcryptjs模块
const bcryptjs = require('bcryptjs')
//导入时间模块
const dayjs = require('dayjs')
//查询员工信息
exports.reqEmpUserInfo = (req, res) => {
    const sql = 'select * from dept where dstatus = 0'
    mysql.query(sql,(err,result)=>{
        const deptInfo = result
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('部门信息查询失败')
        const sql = 'select * from employee left join dept on employee.did = dept.did  where estatus = 0 and dstatus = 0'
        mysql.query(sql, (err, result) => {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('员工信息查询失败')
            const total = result.length
            const sql = 'select * from employee left join dept on employee.did = dept.did where estatus = 0 and dstatus = 0 limit ?,?'
            const page = parseInt(req.params.page)
            const limit = parseInt(req.params.limit)
            const skPage = (page - 1) * limit
            mysql.query(sql, [skPage, limit], (err, result) => {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('员工信息查询失败')
                res.send({ status: 0, message: '员工信息查询成功', data: result, page, limit, total,deptInfo,use:0 })
            })
        })
    })
}
//在职员工
exports.reqSelectUseEmp = (req, res) => {
    const sql = 'select * from dept where dstatus = 0'
    mysql.query(sql,(err,result)=>{
        const deptInfo = result
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('部门信息查询失败')
        const sql = 'select * from employee left join dept on employee.did = dept.did  where estatus = 0 and dstatus = 0'
        mysql.query(sql, (err, result) => {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('员工信息查询失败')
            const total = result.length
            const sql = 'select * from employee left join dept on employee.did = dept.did where estatus = 0 and dstatus = 0 limit ?,?'
            const page = parseInt(req.params.page)
            const limit = parseInt(req.params.limit)
            const skPage = (page - 1) * limit
            mysql.query(sql, [skPage, limit], (err, result) => {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('员工信息查询失败')
                res.send({ status: 0, message: '员工信息查询成功', data: result, page, limit, total,deptInfo })
            })
        })
    })
}
//离职员工
exports.reqSelectDnotUseEmp = (req, res) => {
    const sql = 'select * from dept where dstatus = 0'
    mysql.query(sql,(err,result)=>{
        const deptInfo = result
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('部门信息查询失败')
        const sql = 'select * from employee left join dept on employee.did = dept.did  where estatus = 1 and dstatus = 0'
        mysql.query(sql, (err, result) => {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('员工信息查询失败')
            const total = result.length
            const sql = 'select * from employee left join dept on employee.did = dept.did where estatus = 1 and dstatus = 0 limit ?,?'
            const page = parseInt(req.params.page)
            const limit = parseInt(req.params.limit)
            const skPage = (page - 1) * limit
            mysql.query(sql, [skPage, limit], (err, result) => {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('员工信息查询失败')
                res.send({ status: 0, message: '员工信息查询成功', data: result, page, limit, total,use:1 })
            })
        })
    })
}
//添加员工
exports.reqAddEmp = (req, res) => {
    const sql = 'select * from employee where ename = ?'
    mysql.query(sql, req.body.ename, (err, result) => {
        if (err) return res.cc(err)
        if (result.length == 1) return res.cc('员工名已存在，请重新输入！')
        const sql = 'insert into employee set ?'
        req.body.epass = bcryptjs.hashSync(req.body.epass, 10)
        mysql.query(sql, req.body, (err, result) => {
            if (err) return res.cc(err)
            if (result.affectedRows != 1) return res.cc('添加员工失败，请重新输入！')
            res.cc('添加员工成功', 0)
        })
    })
}
//在职模糊查询
exports.reqFuzzySelect = (req, res) => {
    const sql = 'select * from employee  left join dept on employee.did = dept.did  where dstatus = 0 and estatus = 0 and realname like "%"?"%" limit ?,?'
    //方式一：
    // const page = parseInt(req.body.page)
    // const limit = parseInt(req.body.limit)
    // const likeName = req.body.likeName
    // const skPage = (page - 1) * limit
    //方式二：
    const page = parseInt(req.params.page)
    const limit = parseInt(req.params.limit)
    const likeName = req.body.likeName
    const skPage = (page - 1) * limit
    mysql.query(sql, [likeName, skPage, limit], (err, result) => {
        const total = result.length
        if (err) return res.cc(err)
        if (result.length < 1 || likeName == '') return res.cc('员工查询失败')
        res.send({ status: 0, message: '员工查询成功', data: result, page, limit, total })
    })
}
//离职模糊查询
exports.reqDnotFuzzySelect = (req, res) => {
    const sql = 'select * from employee  left join dept on employee.did = dept.did  where dstatus = 0 and estatus = 1 and realname like "%"?"%" limit ?,?'
    const page = parseInt(req.params.page)
    const limit = parseInt(req.params.limit)
    const skPage = (page - 1) * limit
    const likeName = req.body.likeName
    mysql.query(sql, [likeName, skPage, limit], (err, result) => {
        const total = result.length
        if (err) return res.cc(err)
        if (result.length < 1 || likeName == '') return res.cc('员工查询失败')
        res.send({ status: 0, message: '员工查询成功', data: result, page, limit, total })
    })
}
//员工编辑
exports.reqEditEmp = (req, res) => {
    const sql = 'update employee set ? where eid = ?'
    req.body.epass = bcryptjs.hashSync(req.body.epass, 10)
    mysql.query(sql, [req.body, req.body.eid], (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows != 1) return res.cc('员工编辑失败，请重新输入！')
        res.cc('员工编辑成功！', 0)
    })
}
//员工删除
exports.reqDeleteEmp = (req, res) => {
    const sql = 'update employee set estatus = 1,leavedate = ? where eid = ?'
    //获取员工离职的时间
    const deleteTime = dayjs().format('YYYY-MM-DD')
    mysql.query(sql,[deleteTime,req.params.eid], (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows != 1) return res.cc('员工删除失败，请重新输入！')
        res.cc('员工删除成功！', 0)
    })
}
//员工撤销
exports.reqDontDeleteEmp = (req, res) => {
    const sql = 'update employee set estatus = 0,leavedate = ? where eid = ?'
    mysql.query(sql, [null,req.params.eid], (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows != 1) return res.cc('撤销员工失败，请重新输入！')
        res.cc('撤销员工成功！', 0)
    })
}